package com.quarkus.db.service;

import io.agroal.api.AgroalDataSource;
import io.quarkus.agroal.DataSource;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author: TongRui乀
 * @Date: 2021/3/21 11:03
 * @description：
 */
@ApplicationScoped
public class DbOperationService {

    @Inject
    @DataSource("pg")
    AgroalDataSource agroalDataSource;


    @Inject
    @DataSource("my")
    AgroalDataSource mysql;


    public String getById(Integer id){

       try {
           Connection connection = mysql.getConnection();

           var sql = "select * from t_user where id = ?";

           PreparedStatement statement = connection.prepareStatement(sql);

           statement.setInt(1, id);

           ResultSet resultSet = statement.executeQuery();

           if(resultSet.next()){
               var idd = resultSet.getInt(1);
               var name = resultSet.getString(2);
               return idd + ", " + name;
           }
       }catch (Exception e){
           e.printStackTrace();
       }

       return "nothing";
    }

}
